home *** CD-ROM | disk | FTP | other *** search
- Path: newshost.lanl.gov!tanmoy
- From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
- Newsgroups: comp.lang.c
- Subject: Re: Memory. How is it organised?
- Date: 04 Mar 1996 15:50:10 GMT
- Organization: Los Alamos National Laboratory
- Message-ID: <TANMOY.96Mar4085010@qcd.lanl.gov>
- References: <4hf5gs$1f7@news.mistral.co.uk>
- NNTP-Posting-Host: qcd.lanl.gov
- Mime-Version: 1.0
- Content-Type: text
- In-reply-to: mikebarnard@mistral.co.uk's message of Mon, 04 Mar 1996 14:12:18 GMT
-
- In article <4hf5gs$1f7@news.mistral.co.uk>
- mikebarnard@mistral.co.uk (Mike Barnard) writes:
-
- <snip>
- MB: I want to find out as much as possible about the stack and
- MB: the heap. I know some of this is machine specific, but not
- MB: all of it.
-
- You know incorrectly: all of it is machine or architecture specific.
-
- MB:
- MB: Suppose I did this?
- MB:
- MB: struct tag
- MB: {
- MB: // Variables taking 100 bytes, for an example
- MB: };
- MB:
- MB: struct tag arrayname[50];
- MB:
- MB: What happens? Is 5000 bytes of memory reserved for the
- MB: contents of the array? Where? Stack, heap or somewhere else?
- MB: Or does the compiler just make a "note" that it will be
- MB: needed then allocate some memory when the array(s) are
- MB: initialised. Again, where? (What is the definition of stack
- MB: or heap?)
-
- All of those answers may be correct. Even on machines on which stack
- and heap make sense, it depends whether the declaration appears inside
- a function or at file scope. If it occurs at file scope, and no later
- initialized declaration is seen, a region of 50 * sizeof(struct tag)
- is reserved for arrayname when the end of translation unit is
- seen. Note that sizeof(struct tag) >= 100 bytes, the compiler can
- leave space between your variables or at the end of the struct for
- performance or other reasons.
-
- If it is at file scope and a later initialized declaration is seen, it
- is that declaration that defines arrayname and guarantees that storage
- is reserved. In both these cases, storage is most likely to be
- generated `on heap', if that concept makes sense. It is actually
- better to think of this as some global region: and reserve heap for
- the dynamically allocatable space: it makes no difference.
-
- If it appears inside a function, it is allocated `on stack', again, if
- that makes sense. It may even not be allocated if the compiler can
- figure out it is not needed. If the compiler can determine that a
- function cannot be called recursively (directly or indirectly), and it
- has large stack variable, the compiler may move it to heap instead.
-
- MB:
- MB: Should I malloc the array? How? That is to say; malloc as a
- MB: part of the array definition, or seperately looping through
- MB: each member of the array and mallocing it after it's been
- MB: defined?
- MB:
-
- Instead of asking vague questions, I would advise you to learn C
- systematically. If you have any real problem when you are learning,
- please post some example code so that we know what we are talking
- about.
-
- MB: Someone said on an irc channel about C++ that malloc is
- MB: obsolete. "Use the new command" he said. Then logged off!!!
- MB: I presume there's something in C++ that relates to this, but
- MB: I don't feel ready for OO yet. I want to learn to walk, not
- MB: run.
-
- Decide whether you want to learn C++ or C. The two are different
- languages. OO is a style of programming: not of coding. Some languages
- may make OO easier than others, in fact some languages may have been
- designed with OO programming in mind, but all these languages we are
- talking about are turing equivalent, and all these languages are
- powerful enough to express OO constructs.
-
- If you decide to learn C++, post to comp.lang.c++ instead.
-
- In any case, since you bring it up, the issue is type safety. malloc
- gives you a region of memory of a specified size to do what you want
- with it. Raw memory is untyped, and as far as C is concerned the only
- problem is that you can manage to treat it as different types at
- different moments and tie yourself into knots. In C++, as objects are
- more complicated things (because of native support for OO), there are
- more possiblitites for problems arising from misuse of malloc.
-
- new is not a keyword in C, so you do not have the option of replacing
- malloc with new.
-
- Cheers
- Tanmoy
- --
- tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
- Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
- Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
- <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
- internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
- fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]
-